//
//  MapSimple.m
//  CarCare
//
//  Created by ileo on 15/1/6.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "MapReGeocodeSimple.h"

#define KCoordinateLat @"KCoordinateLat"
#define KCoordinateLng @"KCoordinateLng"
#define KRequestFinish @"KRequestFinish"
#define KRequestError @"KRequestError"

@interface MapReGeocodeSimple() <AMapSearchDelegate>

@property (nonatomic, strong) AMapSearchAPI *search;
@property (nonatomic, strong) NSMutableArray *requestReGeocodes;

@end

@implementation MapReGeocodeSimple

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.search = [[AMapSearchAPI alloc] init];
        self.search.delegate = self;
        self.requestReGeocodes = [NSMutableArray arrayWithCapacity:5];
    }
    return self;
}

-(void)reGeoCodeCoordinate:(CLLocationCoordinate2D)coordinate finish:(void (^)(NSString *, AMapReGeocode *))finish error:(void (^)())error{
    NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithCapacity:3];
    [tmpDic setObject:@(coordinate.latitude) forKey:KCoordinateLat];
    [tmpDic setObject:@(coordinate.longitude) forKey:KCoordinateLng];
    if (finish) {
        [tmpDic setObject:[finish copy] forKey:KRequestFinish];
    }
    if (error) {
        [tmpDic setObject:[error copy] forKey:KRequestError];
    }
    [self.requestReGeocodes addObject:tmpDic];
    [self request];
}

-(void)geoCodeAddress:(NSString *)address finish:(void (^)(NSArray *))finish error:(void (^)())error{
    AMapGeocodeSearchRequest *request = [[AMapGeocodeSearchRequest alloc] init];
    request.address = address;
    [self.search AMapGeocodeSearch:request];
}

-(void)request{
    if (self.requestReGeocodes.count == 0) {
        return;
    }
    NSDictionary *dic = [self.requestReGeocodes firstObject];
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.radius = 250;
    regeo.location = [AMapGeoPoint locationWithLatitude:[dic[KCoordinateLat] doubleValue] longitude:[dic[KCoordinateLng] doubleValue]];
    regeo.requireExtension = YES;
    [self.search AMapReGoecodeSearch:regeo];
}

#pragma mark - search
-(void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    void (^Error)() = [self.requestReGeocodes firstObject][KRequestError];
    DLog(@"map search request fail : %@",error);
    if (Error) {
        Error();
    }
    if (self.requestReGeocodes.count > 0) {
        [self.requestReGeocodes removeObjectAtIndex:0];
        [self request];
    }
}

-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{

    if (response.regeocode)
    {
        void (^Finish)(NSString *poi, AMapReGeocode *reGeocode) = [self.requestReGeocodes firstObject][KRequestFinish];
        if (Finish) {
            Finish([MapHelper addressRoughlyFromReGeocode:response.regeocode],response.regeocode);
        }
    }
    
    if (self.requestReGeocodes.count > 0) {
        [self.requestReGeocodes removeObjectAtIndex:0];
        [self request];
    }
}

-(void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{
    
}

@end